using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace MKDS_Course_Editor
{
    public partial class FrameControl : UserControl
    {
        private const int WM_SCROLL = 276; // Horizontal scroll
        private const int WM_VSCROLL = 277; // Vertical scroll
        private const int SB_LINEUP = 0; // Scrolls one line up
        private const int SB_LINELEFT = 0;// Scrolls one cell left
        private const int SB_LINEDOWN = 1; // Scrolls one line down
        private const int SB_LINERIGHT = 1;// Scrolls one cell right
        private const int SB_PAGEUP = 2; // Scrolls one page up
        private const int SB_PAGELEFT = 2;// Scrolls one page left
        private const int SB_PAGEDOWN = 3; // Scrolls one page down
        private const int SB_PAGERIGTH = 3; // Scrolls one page right
        private const int SB_PAGETOP = 6; // Scrolls to the upper left
        private const int SB_LEFT = 6; // Scrolls to the left
        private const int SB_PAGEBOTTOM = 7; // Scrolls to the upper right
        private const int SB_RIGHT = 7; // Scrolls to the right
        private const int SB_ENDSCROLL = 8; // Ends scroll

        [DllImport("user32.dll",CharSet=CharSet.Auto)]
private static extern int SendMessage(IntPtr hWnd, int wMsg,IntPtr wParam, IntPtr lParam);

        public FrameControl()
        {
            InitializeComponent();
            this.DoubleBuffered = true;
            toolStrip1.Renderer = new TestStripRenderer();
            hScrollBar1.Scroll += new ScrollEventHandler(hScrollBar1_Scroll);
        }

        private void FrameControl_Load(object sender, EventArgs e)
        {
            update();
        }
        public void update()
        {
            panel2.Controls.Clear();
            hScrollBar1.Value = 0;
            for (int i = 0; i < Frames.Count; i++)
            {
                Frames[i].Left = i * 69 + 2;
                Frames[i].Top = 1;
                panel2.Controls.Add(Frames[i]);
            }
            if (Frames.Count * 69 > panel2.Width)
            {
                hScrollBar1.Enabled = true;
                hScrollBar1.Maximum = ((int)((float)(Frames.Count * 69) / (float)panel2.Width * (float)panel2.Width) - panel2.Width + 18) / 2;
            }
        }
        public event EventHandler IndexChanged;
        private int select;
        public int selected
        {
            get { return select; }
            set
            {
                select = value;
                if (this.IndexChanged != null)
                    this.IndexChanged(this, new EventArgs());
            }
        }
        void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
        {
            //panel2.HorizontalScroll.Value = e.NewValue;
            int n = e.NewValue - e.OldValue;
            foreach (AnimFrame a in panel2.Controls)
            {
                a.Left -= n * 2;
            }
           // int n = e.NewValue - e.OldValue;
            //panel2.DisplayRectangle.Offset(-(n*2), 100);
           // SendMessage(panel2.Handle, WM_SCROLL, (IntPtr)SB_RIGHT, IntPtr.Zero);
        }
        public List<AnimFrame> Frames = new List<AnimFrame>();
        public class AnimFrame : Panel
        {
            public AnimFrame(Image i, string t, bool selected, FrameControl parent)
            {
                Image b = new Bitmap(52, 52);
                using (Graphics g = Graphics.FromImage(b))
                {
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                    double xscale = (double)b.Width / (double)i.Width;
                    double yscale = (double)b.Height / (double)i.Height;
                    double scale = xscale;
                    if (yscale < xscale)
                        scale = yscale;
                    int scaleWidth = (int)Math.Floor((double)i.Width * scale);
                    int scaleHeight = (int)Math.Floor((double)i.Height * scale);
                    //Image scaledImage = i.getScaledInstance(scaleWidth, scaleHeight, 16);
                    int x = (b.Width - scaleWidth) / 2;
                    int y = (b.Height - scaleHeight) / 2;
                    g.DrawImage(i, x, y, scaleWidth, scaleHeight);
                }
                this.i = b;
                this.t = t;
                this.selected = selected;
                this.Width = 69;
                this.Height = 80;
                this.parent = parent;
                parent.IndexChanged += new EventHandler(parent_IndexChanged);
                MouseHover += new EventHandler(AnimFrame_MouseHover);
                MouseClick += new MouseEventHandler(AnimFrame_MouseClick);
                MouseLeave += new EventHandler(AnimFrame_MouseLeave);
            }

            void parent_IndexChanged(object sender, EventArgs e)
            {
                this.Invalidate();
            }

            void AnimFrame_MouseLeave(object sender, EventArgs e)
            {
               // this.Cursor = Cursors.Default;
            }

            void AnimFrame_MouseClick(object sender, MouseEventArgs e)
            {
                parent.selected = this.Parent.Controls.IndexOf(this);
            }

            void AnimFrame_MouseHover(object sender, EventArgs e)
            {
                this.Cursor = Cursors.Hand;
            }
            FrameControl parent;
            protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);
                Color BG = Color.FromArgb(237, 237, 237);
                Color bg = BG;
                Color outline = Color.FromArgb(200, 200, 200);
                Color border = Color.FromArgb(160, 160, 160);
                Color text = Color.FromArgb(30, 30, 30);
                Color SELECTED = Color.FromArgb(51, 153, 255);
                SolidBrush b = new SolidBrush(Color.Black);
                if (parent.selected == this.Parent.Controls.IndexOf(this))
                {
                    bg = SELECTED;
                    outline = Color.White;
                    text = Color.White;
                }
                b.Color = bg;
                e.Graphics.FillRectangle(b, 0, 0, 69, 80 - 1);
                int width = 69 - 2;
                b.Color = Color.FromArgb(160, 160, 160);
                e.Graphics.DrawLine(new Pen(b), 69 - 1, 0, width + 1, 80);
                int size = 4;
                b.Color = outline;
                e.Graphics.DrawRectangle(new Pen(b), size, size, width - size * 2, width - size * 2);
                size = 7;
                e.Graphics.DrawImage(drawGrid(4, 4, width - size * 2, width - size * 2), size + 1, size + 1);
                e.Graphics.DrawImage(i, size + 1, size + 1);
                String num = (this.Parent.Controls.IndexOf(this) + 1).ToString();// t;//(new StringBuilder()).Append("").Append(getIndex() + 1).toString();
                int numWidth = num.Length * 5 + 7;
                int txtWidth = t.Length * 5 + 7;
                b.Color = BG;
                e.Graphics.FillRectangle(b, size, size, numWidth, 14);
                b.Color = border;
                e.Graphics.DrawRectangle(new Pen(b), size, size, width - size * 2, width - size * 2);
                e.Graphics.DrawRectangle(new Pen(b), size, size, numWidth, 14);
                b.Color = Color.FromArgb(100, 100, 100);
                e.Graphics.DrawString(num, new Font(new FontFamily("Tahoma"), 9),b, size, size /*+ 11*/);
                b.Color = text;
                e.Graphics.DrawString(t, new Font(new FontFamily("Tahoma"), 9), b, /*size + 2*/ (Width/2) - (txtWidth/2), /*size +*/ width - 4/* + 1*/);
            }

            public Image i;
            public string t;
            public bool selected;
        }
        public class TestStripRenderer : ToolStripSystemRenderer
        {
            protected override void OnRenderToolStripBackground(ToolStripRenderEventArgs e)
            {
                e.Graphics.FillRectangle(new SolidBrush(e.BackColor), 0, 0, e.ToolStrip.Width, e.ToolStrip.Height);
            }
            protected override void OnRenderToolStripBorder(ToolStripRenderEventArgs e)
            {
            }
        }
        public static Image drawGrid(int tw, int th, int width, int height)
        {
            Image grid = new Bitmap(width, height);
            Color COLOR_BG = Color.FromArgb(255, 255, 255);
            Color COLOR_GRID = Color.FromArgb(204, 204, 204);

            using (Graphics g = Graphics.FromImage(grid))
            {
                g.FillRectangle(new SolidBrush(COLOR_BG), 0, 0, width, height);
                int cols = (int)Math.Ceiling((double)width / (double)tw);
                int rows = (int)Math.Ceiling((double)height / (double)th);
                for (int y = 0; y < rows; y++)
                {
                    for (int x = 0; x < cols; x++)
                    {
                        bool even = x % 2 == 1 && y % 2 == 0;
                        bool odd = x % 2 == 0 && y % 2 == 1;
                        if (even || odd)
                            g.FillRectangle(new SolidBrush(COLOR_GRID), x * tw, y * th, tw, th);
                    }

                }
            }
            return grid;
        }

        private void panel2_Paint(object sender, PaintEventArgs e)
        {
            Color outline = Color.FromArgb(160, 160, 160);
            //e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(237, 237, 237)), 0, 0, panel2.Width, 80);
            e.Graphics.DrawLine(new Pen(outline), 2,0, panel2.Width - 2,0);
            e.Graphics.DrawLine(new Pen(outline), 2, 81, panel2.Width - 2, 81);
        }

        private void FrameControl_Resize(object sender, EventArgs e)
        {
            if (Frames.Count * 69 > panel2.Width)
            {
                hScrollBar1.Enabled = true;
                hScrollBar1.Maximum = ((int)((float)(Frames.Count * 69) / (float)panel2.Width * (float)panel2.Width) - panel2.Width + 18 )/ 2;
            }
            else
            {
                hScrollBar1.Enabled = false;
            }
        }
    }
}
